package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.model.behavior.dtos.ReadBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.constants.article.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;
    /**
     * 记录阅读行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDTO dto) {
        if (dto.getArticleId()==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"文章id不能为空");
        }
        //得到登录用户的id
        ApUser user = AppThreadLocalUtils.getUser();

        //得到收集或用户的信息
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());


        //查找本用户是不是查看过本文章
        Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
        ApReadBehavior readBehavior = mongoTemplate.findOne(query, ApReadBehavior.class);
        //如果为空说明时第一次查看,创建查看的数据
        if (readBehavior==null){
            ApReadBehavior apReadBehavior = new ApReadBehavior();
            apReadBehavior.setEntryId(apBehaviorEntry.getId());
            apReadBehavior.setArticleId(dto.getArticleId());
            apReadBehavior.setCreatedTime(apBehaviorEntry.getCreatedTime());
            apReadBehavior.setUpdatedTime(new Date());
            apReadBehavior.setCount((short) 1);
            mongoTemplate.save(apReadBehavior);
        }else {
            //为已经查看过查看次数加以
            readBehavior.setCount((short) ((short) 1+ readBehavior.getCount()));
            //跟新时间
            readBehavior.setUpdatedTime(new Date());
            mongoTemplate.save(readBehavior);
        }

        //发送新增行为消息,用于热带你文章实时计算
        NewBehaviorDTO behaviorDTO = new NewBehaviorDTO();
        behaviorDTO.setType(NewBehaviorDTO.BehaviorType.VIEWS);
        behaviorDTO.setArticleId(dto.getArticleId());
        behaviorDTO.setAdd(1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE,
                JSON.toJSONString(behaviorDTO)
        );
        log.info("成功发送阅读行为消息 ==> 当前文章id:{} ==> 消息内容 {}",dto.getArticleId(),JSON.toJSONString(behaviorDTO));

        return ResponseResult.okResult();
    }
}
